package com.haoyi.feignapi.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.haoyi.feignapi.entity.Attachment;
import com.haoyi.feignapi.entity.Order;
import com.haoyi.feignapi.entity.User;
import com.haoyi.feignapi.entity.requestBody.PartReq;
import com.haoyi.feignapi.mapper.EnMallMapper;
import com.haoyi.feignapi.mapper.ErpMapper;
import com.haoyi.feignapi.service.EnMallService;
import com.haoyi.feignapi.service.ErpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestBody;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class, propagation = Propagation.REQUIRED)
@DS("enmall")
public class EnMallServiceImpl implements EnMallService {

    final
    EnMallMapper enMallMapper;

    public EnMallServiceImpl(EnMallMapper enMallMapper) {
        this.enMallMapper = enMallMapper;
    }

    @Override
    public PageInfo<Map<String, Object>> getOrder(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> order_list = enMallMapper.getOrder(paramMap);
        return new PageInfo<>(order_list);
    }

    @Override
    public Object getOrderParts1(Map<String, Object> paramMap) {
        List<Map<String, Object>> list = enMallMapper.getOrderParts1(paramMap);
        return list;
    }

    @Override
    public List<Order> getOrderEN(String v2, String v3, String v4, String v5) {
        return enMallMapper.getOrderEN(v2, v3, v4, v5);
    }

    @Override
    public void signinOverseasUser(User user) {
        enMallMapper.signinOverseasUser(user);
    }

    @Override
    public List<User> getUserBySearch(Integer permission_id, String search) {
        return enMallMapper.getUserBySearch(permission_id, search);
    }

    @Override
    public List<User> getUserByPermission(Integer permission_id) {
        return enMallMapper.getUserByPermission(permission_id);
    }

    @Override
    public Object getAttachment(PartReq partReq) {
        String type_sql = "";
        if (Objects.equals(partReq.getPartType(), "P")) {
            type_sql = "AND B.part_code like '7%'";
        } else if (Objects.equals(partReq.getPartType(), "B")) {
            type_sql = "AND B.part_code like '8%'";
        } else if (Objects.equals(partReq.getPartType(), "N")) {
            type_sql = "AND B.part_code like '8%'";
        }
        System.out.println(partReq.getCurrentPage() + "-----------" + partReq.getPageSize());
        PageHelper.startPage(partReq.getCurrentPage(), partReq.getPageSize());
        EnMallServiceImpl attachmentMapper;
        PageInfo<Attachment> attachmentPage = new PageInfo<>(enMallMapper.getAttachment(partReq.getMachineId(), type_sql, "%" + partReq.getSearch() + "%"));
        return new ResponseEntity<>(attachmentPage, HttpStatus.OK);
    }

    @Override
    public List<Map<String, Object>> getOrderParts(Map<String, Object> paramMap) {
        return enMallMapper.getOrderParts(paramMap);
    }

    @Override
    public Map<String, Object> orderProcessing(Map<String, Object> paramMap) {
        Integer checkBsUserIdNum = enMallMapper.processingCheckBsUserId(paramMap);
        Map<String, Object> resultMap = new HashMap<>();
        if (checkBsUserIdNum > 0) {
            enMallMapper.setOrderStatus(paramMap.get("order_code").toString(), 4, "status_time_04");
            resultMap.put("status", "success");
        } else {
            resultMap.put("status", "warn");
            resultMap.put("message", "你无权处理该订单");
        }
        return resultMap;
    }

    @Override
    public PageInfo<Map<String, Object>> getUserData(Map<String, Object> paramMap) {
        Integer currPage = (Integer) paramMap.get("currPage");
        Integer pageSize = (Integer) paramMap.get("pageSize");
        PageHelper.startPage(currPage, pageSize);
        List<Map<String, Object>> user_list = enMallMapper.getUserData(paramMap);
        return new PageInfo<>(user_list);
    }

    @Override
    public Integer getAllTradeCount(Date date1, Date date2) {
        return enMallMapper.getAllTradeCount(date1, date2);
    }


}
